<?php
namespace app\configs\admin;

use app\admin\controller\Admin;
use think\Db;

use app\common\builder\ZBuilder; // 引入ZBuilder

class Job extends Admin
{
    protected  $status=[
        0=>'未读',
        1=>'待定',
        2=>'不合适',
        3=>'待面试',
        4=>'待入职',
        5=>'待满岗',
        6=>'未满岗',
        10=>'满岗'];

	//招聘列表
    public function index(){
		
        $map = $this->getMap();
        
        $status=$this->request->param("status",null);
        $hot=$this->request->param("hot",null);
        
        if($status){
            $map[]=['a.status','=',1];
        }
        if($hot){
            $map[]=['a.hot','=',1];
        }
        
        $data_list = Db::name('zp_job')
        ->alias("a")
        ->join("zp_category b","a.category=b.id",'left')
        ->join("zp_area c","a.province_id=c.id",'left')
        ->join("zp_area d","a.city_id=d.id",'left')
        ->join("zp_area e","a.region_id=e.id",'left')
        ->where($map)
        ->where("jz",0)
        ->field("a.*,b.name cate_name,c.fullname province_name,d.fullname city_name,e.fullname region_name")
        ->order("a.hot desc,a.update_time desc,a.id desc")
        ->paginate();
        
        // 分页数据
        $page = $data_list->render();
        $category=Db::name('zp_category')->column("id,name");
        
        $btn_add = [
            'title' => '发布职位',
            'icon'  => 'fa fa-fw fa-plus',
            'href'  => url('job_add')
        ];
        $all=Db::name('zp_job')->where("jz",0)->count("id");
        $sta1=Db::name('zp_job')->where("jz",0)->where("status",1)->count("id");
        $stahot=Db::name('zp_job')->where("jz",0)->where("hot",1)->count("id");
        
        $btn_all = [
            'title' => '总职位:'.$all."条",
            'href'  =>  url('index')
        ];
        
        $btn_sta1 = [
            'title' => '上架'.$sta1."条",
            'href'  => url('index',['status'=>1])
        ];
        
        
        $btn_stahot = [
            'title' => '热门'.$stahot."条",
            'href'  => url('index',['hot'=>1])
        ];
        
        $btn_edit = [
            'title' => '修改职位',
            'icon'  => 'fa fa-fw fa-pencil',
            'href'  => url('job_edit', ['id' => '__id__'])
        ];
        
        $btn_resume = [
            'title' => '简历总数',
            'icon'  => 'fa fa-fw fa-th',
            'href'  => url('resume_list', ['id' => '__id__'])
        ];
        
        $css = <<<EOF
           <style>
                .table-builder > tbody > tr > td{white-space: normal;}
           </style>
EOF;
        
        $html = <<<EOF
            <p></p>
EOF;
        //		dump($data_list);die;
        //$zp_category=Db::name("zp_category")->column("id,name");
        return ZBuilder::make('table')
        ->setPageTitle('职位管理')
        ->setTableName('zp_job') // 指定数据表名
        ->setPrimaryKey('id')	//设置主键
        ->hideCheckbox()
        ->setSearch(['a.title' => '标题']) // 设置搜索参数
        ->addColumns([ // 批量添加列
            ['title', '标题','text.edit'],
            ['category', '职位类别','callback',function($value){
                $name=Db::name("zp_category")->where("id","in",$value)->column("name");
                $str=implode("<br>", $name);
                return $str;
            }, '__data__'],
            ['province', '工作地点','callback',function($data){
                return $data['province_name'].$data['city_name'].$data['region_name'].$data['address'];
            }, '__data__'],
            ['money', '薪资','text.edit'],
            ['company', '公司','text.edit'],
            ['point', '积分','text.edit'],
            ['resume', '简历','callback',function($data){
                $a=Db::name("zp_resume_job")->where("job_id",$data['id'])->count("id");
                return $a;
            }, '__data__'],
            ['views', '访客'],
            ['hot', '是否热门','switch'],
            ['status','上架/下架','switch'],
            ['tag', '福利','callback',function($data){
                $a=Db::name("config_benefits")->where("id","in",$data['tags'])->column("name");
                return implode(",", $a);
            }, '__data__'],
           
            //['city_name', '市'],
            //['region_name', '区县'],
            //['address', '地址','text.edit'],
            
            ['contact', '联系人','text.edit'],
            ['phone', '电话','text.edit'],
            
            //['point2', '入职分','text.edit'],
           // ['cover', '封面图','picture'],
            ['create_time', '添加时间'],
           // ['content', '文字内容','popover'],
        ])
        ->addColumn('right_button', '操作', 'btn')
        ->addRightButton('btn_resume',$btn_resume,1) // 批量添加右侧按钮1
        

        ->addTopButton('btn_add',$btn_add) // 批量添加右侧按钮1
        
        ->addTopButton('btn_all',$btn_all) // 批量添加右侧按钮1
        ->addTopButton('sta1',$btn_sta1) // 批量添加右侧按钮1
        ->addTopButton('stahot',$btn_stahot) // 批量添加右侧按钮1
        
        ->addRightButton('btn_edit',$btn_edit) // 批量添加右侧按钮1
        ->addRightButton('delete') // 批量添加右侧按钮1
        
        ->setRowList($data_list) // 设置表格数据
        ->setPages($page) // 设置分页数据
        ->setExtraCss($css)
        ->setExtraHtml($html,'toolbar_top')
        ->fetch();
    }
	
	
    
    public function job_add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            halt($data);
            $data['create_time']=date("Y-m-d H:i:s");
            $data['update_time']=date("Y-m-d H:i:s");
            if(!$data['region_id']){
                return $this->error('请选择地区');
            }
            
            if(!isset($data['category'])){
                return $this->error('请选择工种和分类');
            }
            
            if(isset($data['tags'])){
                $data['tags']=implode(",",$data['tags']);
            }
            $data['category']=implode(",",$data['category']);
            if(isset($data['categorys'])){
                $data['categorys']=implode(",",$data['categorys']);
            }
            $region_pid=Db::name("zp_area")->find($data['region_id']);
            $city_pid=Db::name("zp_area")->where("id",$region_pid['pid'])->value("pid");
            $data['city_id']=$region_pid['pid'];
            $data['province_id']=$city_pid;
            
            $data['lat']=$region_pid['lat'];
            $data['lng']=$region_pid['lng'];
            
            $r=db('zp_job')->insert($data);
            //行为管理
            action_log('添加招聘', 'configs', "1", UID, session('user_auth')['nickname']."添加招聘");
            
            if($r){
                return $this->success('添加成功',url("index"));
            }else {
                return $this->error('添加失败',url("index"));
            }
        }
        //$list_province=Db::name("zp_area")->where("pid",100000)->column("name","id");
        $category=Db::name("zp_category")->where("pid",0)->column("id,name");
        $categorys=Db::name("zp_category_qt")->where("pid",0)->column("id,name");
        
        $config_benefits=Db::name("config_benefits")->where("pid",0)->order("order,pid")->column("id,name");
        
//         $da1=[];
//         foreach ($config_benefits as $k=>$v){
//             $a=Db::name("config_benefits")->where("pid",$k)->order("pid")->column("id,name");
//             $config_benefits=array_merge($config_benefits,$a);
           
//         }
        
        //halt($list_province);
        // 使用ZBuilder快速创建表单
        $arr=[ // 批量添加表单项
            ['text', 'title', '招聘标题', ''],
            ['image', 'cover', '封面图'],
            //['linkages', 'category', '所属分类','','zp_category',2],
            // ['checkbox', 'category', '所属分类','',$category],
            ['linkage', 'category1', '选择分类','',$category,'',url('get_cate'), 'category', 'category'],
            ['select', 'category', '选择工种', '','','', 'multiple'],];
        

        
        foreach ($categorys as $k=>$v){
            $val=[];
            $val=Db::name("zp_category_qt")->where("pid",$k)->column("id,name");
            $arr[]=['checkbox', 'categorys_'.$k, $v,'',$val];
        }
        
        return ZBuilder::make('form')
        ->setPageTitle('新增') // 设置页面标题
        ->addFormItems(
            $arr
            )
            
            
            
            ->addFormItems([ 
            
           // ['linkage', 'categorys1', '选择其它分类','',$categorys,'',url('get_cates'), 'categorys', 'categorys'],
           // ['select', 'categorys', '选择其它', '','','', 'multiple'],
            
            ['checkbox', 'tags', '福利标签','',$config_benefits],
            ['text', 'company', '公司名称'],
            ['text', 'money', '待遇'],
            ['text', 'contact', '联系人'],
            ['text', 'phone', '联系电话'],
            ['number', 'point', '积分奖励'],
            ['number', 'p0', '稳岗奖励比例','填写百分比',50],
            ['number', 'p1', '一级推广奖励比例','填写百分比',30],
            ['number', 'p2', '二级推广奖励比例','填写百分比',20],
            ['text', 'point_tj', '积分领取条件','','从入职开始计算在职50天（实际考勤不低于350小时）；自离、旷离、严重违纪开除均无积分奖励'],
            ['text', 'point_time', '积分领取时间','','满足条件后10-15个工作日积分到账'],
            
            
            
//             ['linkage', 'province_id', '选择省份','',$list_province,'510000',url('get_city'), 'city_id,region_id', 'province'],
//             ['linkage', 'city_id', '选择城市', '', '', '', url('get_area'), 'region_id'],
            ['linkages', 'region_id', '选择所在地区', '', 'zp_area', 4,510105],
            ['text', 'address', '详细地址'],
            ['text', 'info_from', '公司介绍'],
           // ['bmap','bmap','地图','lDneqLvRX2mnQloI1KhkjYXi', '', '104.234923,30.529877'],
            ['wangeditor', 'content', '招聘详情'],
           
        ])
        ->layout([
            "title"=>6,
            "category"=>6,
            "cover"=>6,
            
//             "province_id"=>3,
//             "city_id"=>3,
            "region_id"=>9,
            "address"=>3,
            
            "money"=>3,
            "contact"=>3,
            "phone"=>3,
            "company"=>3,
            "point"=>3,
            "point2"=>3,
            "category1"=>3,
            "category"=>3,
            "categorys1"=>3,
            "categorys"=>3,
            "p0"=>3,
            "p1"=>3,
            "p2"=>3,
        ])
        ->isAjax(false)
        ->fetch();
    }
    public function get_cate($category)
    {
        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $list=Db::name("zp_category")->where("pid",$category)->column("name","id"); //数据
        $ls=[];
        foreach ($list as $k=>$v){
            $ls[$k]['key']=$k;
            $ls[$k]['value']=$v;
        }
        $arr['list'] =$ls;
        return json($arr);
    }
    
    public function get_cates($categorys)
    {
        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $list=Db::name("zp_category_qt")->where("pid",$categorys)->column("name","id"); //数据
        $ls=[];
        foreach ($list as $k=>$v){
            $ls[$k]['key']=$k;
            $ls[$k]['value']=$v;
        }
        $arr['list'] =$ls;
        return json($arr);
    }
    
    // 根据省份获取城市
    public function get_city($province = 510000)
    {
        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
         $list=Db::name("zp_area")->where("pid",$province)->column("name","id");; //数据
         $ls=[];
         foreach ($list as $k=>$v){
             $ls[$k]['key']=$k;
             $ls[$k]['value']=$v;
         }
         $arr['list'] =$ls;
        return json($arr);
    }
    
    public function get_area($city_id = '')
    {
        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $list=Db::name("zp_area")->where("pid",$city_id)->column("name","id");; //数据
        $ls=[];
        foreach ($list as $k=>$v){
            $ls[$k]['key']=$k;
            $ls[$k]['value']=$v;
        }
        $arr['list'] =$ls;
        return json($arr);
    }
    
    public function job_edit($id=null){
        if ($id === null) $this->error('缺少参数');
        $info = db('zp_job')->find($id);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if(!$data['region_id']){
                return $this->error('请选择地区');
            }
            
            if(!isset($data['category'])){
                return $this->error('请选择工种和分类');
            }
            $data['category']=implode(",",$data['category']);
            if(isset($data['categorys'])){
                $data['categorys']=implode(",",$data['categorys']);
            }
            $region_pid=Db::name("zp_area")->find($data['region_id']);
            $city_pid=Db::name("zp_area")->where("id",$region_pid['pid'])->value("pid");
            $data['city_id']=$region_pid['pid'];
            $data['province_id']=$city_pid;

            $data['lat']=$region_pid['lat'];
            $data['lng']=$region_pid['lng'];
            
            $data['update_time']=date("Y-m-d H:i:s");
            $r=db('zp_job')->where("id",$id)->update($data);
            action_log('修改招聘', 'configs', "1", UID, session('user_auth')['nickname']."修改招聘");
            if($r){
                return $this->success('修改成功',url('index'));
            }else {
                return $this->error('修改失败',url('index'));
            }
        }
       // $list_province=Db::name("zp_area")->where("pid",100000)->column("name","id");
        $category=Db::name("zp_category")->where("pid",0)->column("id,name");
        $categorys=Db::name("zp_category_qt")->where("pid",0)->column("id,name");
        $config_benefits=Db::name("config_benefits")->where("pid",0)->order("order,pid")->column("id,name");
        
//         $da1=[];
//         foreach ($config_benefits as $k=>$v){
//             $a=Db::name("config_benefits")->where("pid",$k)->order("pid")->column("id,name");
//             $config_benefits=array_merge($config_benefits,$a);
            
//         }
        return ZBuilder::make('form')
        ->setPageTitle('编辑') // 设置页面标题
        ->addFormItems([ // 批量添加表单项
            ['hidden', 'id'],
            ['text', 'title', '招聘标题', ''],
            ['image', 'cover', '封面图'],
           // ['linkages', 'category', '所属分类','','zp_category',2],
            ['linkage', 'category1', '选择分类','',$category,'',url('get_cate'), 'category', 'category'],
            ['select', 'category', '选择工种', '',$category,'', 'multiple'],
            
            ['linkage', 'categorys1', '选择其它分类','',$categorys,'',url('get_cates'), 'categorys', 'categorys'],
            ['select', 'categorys', '选择其它', '','','', 'multiple'],
           
            ['checkbox', 'tags', '福利标签','',$config_benefits],
            ['text', 'company', '公司名称'],
            ['text', 'money', '待遇'],
            ['text', 'contact', '联系人'],
            ['text', 'phone', '联系电话'],
            ['number', 'point', '积分奖励'],
            ['number', 'p0', '稳岗奖励比例','填写百分比',50],
            ['number', 'p1', '一级推广奖励比例','填写百分比',30],
            ['number', 'p2', '二级推广奖励比例','填写百分比',20],
            ['text', 'point_tj', '积分领取条件'],
            ['text', 'point_time', '积分领取时间'],
            //             ['linkage', 'province_id', '选择省份','',$list_province,'510000',url('get_city'), 'city_id,region_id', 'province'],
        //             ['linkage', 'city_id', '选择城市', '', '', '', url('get_area'), 'region_id'],
            ['linkages', 'region_id', '选择所在地区', '', 'zp_area', 4],
            ['text', 'address', '详细地址'],
            ['text', 'info_from', '公司介绍'],
            ['wangeditor', 'content', '招聘详情'],
            
        ])
        ->layout([
            "title"=>6,
            "category"=>6,
            "cover"=>6,
            
            //             "province_id"=>3,
        //             "city_id"=>3,
            "region_id"=>9,
            "address"=>3,
            
            "money"=>3,
            "contact"=>3,
            "phone"=>3,
            "company"=>3,
            "point"=>3,
            "point2"=>3,
            "category1"=>3,
            "category"=>3,
            "categorys1"=>3,
            "categorys"=>3,
            "p0"=>3,
            "p1"=>3,
            "p2"=>3,
        ])->setFormData($info) // 设置表单数据
        //->isAjax(false)
        ->fetch();
        
    }
    
    
    public function resume_list($id){
        $map = $this->getMap();
        $map[]=['a.job_id','=',$id];
        $data_list = Db::name('zp_resume_job')
        ->alias("a")
        ->join("zp_resume b","a.resume_id=b.id")
        ->where($map)
        ->field("a.id aid,a.status astatus,a.create_time acreate_time,b.*")
        ->order("aid desc")
        ->paginate();
        // 分页数据
        $page = $data_list->render();

        $btn_add = [
            'title' => '稳岗奖励',
            'icon'  => 'fa fa-fw fa-heart',
            'class' => 'btn btn-xs btn-default ajax-get confirm',
            'data-title' => '确定要稳岗操作吗？操作后不可撤销',
            'href'  => url('resume_job', ['id' => '__aid__','job_id' => $id,'user_id' => '__user_id__'])
        ];
        
        $css = <<<EOF
           <style>
                .table-builder > tbody > tr > td{white-space: normal;}
           </style>
EOF;
        //		dump($data_list);die;
        $zp_category=Db::name("zp_category")->column("id,name");
        return ZBuilder::make('table')
        ->setPageTitle('投递列表')
        ->setTableName('zp_resume_job') // 指定数据表名
        ->setPrimaryKey('aid')	//设置主键
        ->hideCheckbox()
        ->setSearch(['b.user_name' => '姓名','b.phone' => '电话']) // 设置搜索参数
        ->addColumns([ // 批量添加列
            ['user_name', '姓名'],
            ['sex', '性别',['0'=>'保密','1'=>'男','2'=>'女']],
            ['birthday', '出生年月'],
            ['phone', '电话'],
            ['minzu', '民族'],
            ['qiw_dd', '期望地点'],
            ['qiw_xz', '期望薪资'],
            ['qiw_zw', '期望职位'],
            ['acreate_time', '投递时间'],
            ['astatus', '是否稳岗',"callback",function($v){
                return $v==10?"是":"否";
            }],
            // ['content', '文字内容','popover'],
            ])
            ->addColumn('right_button', '操作', 'btn')
            ->addRightButton('btn_add',$btn_add) // 批量添加右侧按钮1
            ->addRightButton('delete') // 批量添加右侧按钮1
            
            ->setRowList($data_list) // 设置表格数据
            ->setPages($page) // 设置分页数据
            ->setExtraCss($css)
            ->fetch();
    }
    
    public function user_message($id,$content=''){
        $r=Db::name('user_message')->insert([
            "content"=>$content,
            "user_id"=>$id,
            "create_time"=>date("Y-m-d H:i:s"),
        ]);
        return $r;
    }
    
    public function resume_job($id,$job_id,$user_id){
        action_log('招聘稳岗', 'configs', "1", UID, session('user_auth')['nickname']."招聘稳岗");
        $status=Db::name("zp_resume_job")->where("id",$id)->value("status");
        if($status==10){
           //return $this->error("已稳岗,无需再次操作");
        }
        $time=date("Y-m-d H:i:s");
        $r=Db::name("zp_resume_job")->where("id",$id)->update([
            "status"=>10,
            "update_time"=>$time,
        ]);
        
        //计算推荐人积分
        $point=Db::name("zp_job")->where("id",$job_id)->value("point");
        if(!$point){
            if($r){
                return $this->success("设置成功");
            }else{
                return $this->error("设置失败");
            }
        }
        
        $job=Db::name("zp_job")->find($job_id);
        
        $user=Db::name("user")->find($user_id);
        //稳岗奖励给自己
       // $wengang=Db::name("admin_config")->where("id",43)->value("value"); //比例
        //写入积分纪录并计算积分
        $wengang=$point*$job['p0']/100;
        $insert_datawengang=[
            "user_id"=>$user_id,
            "point"=>(int)$wengang,
            "create_time"=>$time,
            "remarks"=>"稳岗奖励，".$user['username']."稳岗成功",
        ];
        Db::name("point_log")->insert($insert_datawengang);
        Db::name("user")->where("id",$user_id)->setInc('point',(int)$wengang);
        
        $status1=Db::name("zp_resume_job")->where("id",$id)->value("status");
        
        $message="您的简历已被修改为：".$this->status[$status1];
        $this->user_message($user_id,$message);
        //查找一级分销人
        $recommend1=Db::name("user")->where("id",$user_id)->value("recommend_id");
        
        if($recommend1){
           // $one=Db::name("admin_config")->where("id",40)->value("value"); //比例
            
            //写入积分纪录并计算积分
            $point1=$point*$job['p1']/100;
            $insert_data1=[
                "user_id"=>$recommend1,
                "point"=>(int)$point1,
                "create_time"=>$time,
                "remarks"=>"一级分销奖励，".$user['username']."稳岗成功",
            ];
            Db::name("point_log")->insert($insert_data1);
            Db::name("user")->where("id",$recommend1)->setInc('point',(int)$point1);
            
            
            $message=$user['username']."的简历已被修改为：".$this->status[$status1];
            $this->user_message($recommend1,$message);
        }
        
        //查找一级二级比例 
        $recommend2=Db::name("user")->where("id",$recommend1)->value("recommend_id");
        if($recommend2){
          //  $two=Db::name("admin_config")->where("id",41)->value("value");
            
            //写入积分纪录并计算积分
            $point2=$point*$job['p2']/100;
            $insert_data2=[
                "user_id"=>$recommend2,
                "point"=>(int)$point2,
                "create_time"=>$time,
                "remarks"=>"二级分销奖励，".$user['username']."稳岗成功",
            ];
            Db::name("point_log")->insert($insert_data2);
            Db::name("user")->where("id",$recommend2)->setInc('point',(int)$point2);
            
            $message=$user['username']."的简历已被修改为：".$this->status[$status1];
            $this->user_message($recommend2,$message);
        }

        if($r){
            return $this->success("设置成功");
        }else{
            return $this->error("设置失败");
        }
        
        
    }
    
    
    
    
    
    
   
}
?>